#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/1/17 23:17
# @Author  : 唐松
# @File    : send_mail.py
# @Software: PyCharm
# from configs.config import read_config_file


import smtplib
from email.header import Header
from email.mime.text import MIMEText
from configs.config import read_config_file
from configs.setting import is_email_msg
from util_tools.logger_util import logs


class SendEmail:
    """ 发送邮箱 """

    def __init__(self):
        # 读取配置文件
        self.email_name = read_config_file('email', 'email_name')
        self.email_password = read_config_file('email', 'email_password')
        self.email_port = read_config_file('email', 'email_port')
        self.email_server = read_config_file('email', 'email_server')
        self.email_receiver = read_config_file('email', 'email_receiver')

    def send_email(self, subject, message):
        if not is_email_msg:
            return
        try:
            # 邮件内容设置
            msg = MIMEText(message, 'plain', 'utf-8')
            msg['From'] = Header(self.email_name)
            msg['To'] = Header(self.email_receiver)
            msg['Subject'] = Header(subject)

            # 连接服务器并发送邮件
            server = smtplib.SMTP_SSL(self.email_server, self.email_port)
            server.login(self.email_name, self.email_password)
            server.sendmail(self.email_name, [self.email_receiver], msg.as_string())
            server.quit()
            logs.info("邮件发送成功")
        except Exception as e:
            logs.error(f'发送邮件失败, 失败原因: {e}')


if __name__ == '__main__':
    testcase = SendEmail()
    testcase.send_email('testcase', '0206')
